/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecomp.uefs.micc.sharedbox.view;





import ecomp.uefs.micc.sharedbox.model.Client;
import ecomp.uefs.micc.sharedbox.model.SharedFile;
import ecomp.uefs.micc.sharedbox.model.User;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author emanuel
 */
public class ClientAppFrame extends javax.swing.JFrame {

    private Client client;
    private User user;
    private Socket socket;
   
    private String email;
    private String password;
    public LoginDialog loginDialog = new LoginDialog(this, rootPaneCheckingEnabled);
       
    
    

    /**
     * Creates new form ClientFrame
     */
    public ClientAppFrame() {
        client = new Client();
        user = new User();
        
        initComponents();
        this.setResizable(false);
        this.setTitle(email);
        setLocationRelativeTo(null);
    }
    
    class ListenerLogin implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            
            if("OK".equals(e.getActionCommand())) {
                user.setEmail(String.valueOf(loginDialog.getTxtEmail().getText()));
                user.setPassword(String.valueOf(loginDialog.getTxtPassword().getText()));
                client.setUser(user);
                client.Login();
                client.running();
                if(client.getAcknoledge() == 13){
                setLog("Logged in " + client.getHOST());
                btnUpload.setEnabled(true);
                
                btnDownload.setEnabled(true);
                btnShare.setEnabled(true);
                showServerFiles();
                //showServerFiles();
                showSharedFiles();
                
                btnLogin.setEnabled(false);
                setTitle();
                btnLogout.setEnabled(true);
                loginDialog.dispose();
            }
                else if (client.getAcknoledge() == 14){
                    setLog(" Senha Errada");
                    loginDialog.getTxtPassword().setText("");
                }
                    else{
                        loginDialog.getTxtEmail().setText("");
                        loginDialog.getTxtPassword().setText("");
                        loginDialog.dispose();
                    }
               
            } else {
                loginDialog.dispose();
            }
        }
        
    }
    
    private void setTitle() {
        this.setTitle(user.getEmail());
    }
    
    private void disconnect() {
        System.out.println("REGISTROU ESSA PORRA");
    }
    
    private void registered() {
        JOptionPane.showMessageDialog(null, "Usuário Cadastrado com Sucesso "
                                          , "Sharedbox Cadastro", 1);
    }
    
    /**
     * 
     */
 
    private void showFiles() {
        DefaultTableModel modelo;
        String path = client.getDir();
        File file = new File(path);
        File afile[] = file.listFiles();
        String[] col = {"File Name", "Update Date"};
        String[][] table = null;
        modelo = new DefaultTableModel(table, col);
        int i = 0;
        for (int j = afile.length; i < j; i++) {
            File arquivo = afile[i];
            modelo.addRow(new Object[]{arquivo.getName(), new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date(arquivo.lastModified()))});

        }
        tableLocal.setModel(modelo);
    }
    
    private void showServerFiles() {
        
        DefaultTableModel modelo;
       
       
        File afile[] = client.getListFilesServer();
        String[] col = {"File Name", "Update Date"};
        String[][] table = null;
        modelo = new DefaultTableModel(table, col);
        int i = 0;
        for (int j = afile.length; i < j; i++) {
            File arquivo = afile[i];
            modelo.addRow(new Object[]{arquivo.getName(),new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").
                    format(new Date()).toUpperCase()});

        }
        tableLocal.setModel(modelo);
        
    }
    
     private void showSharedFiles() {
        
        DefaultTableModel modelo;
        ArrayList<SharedFile> shlist = client.getUser().getArquivosCompartilhados();
       
        File afile[] = client.getListFilesServer();
        String[] col = {"File Name", "Proprietário", "Modificado em"};
        String[][] table = null;
        modelo = new DefaultTableModel(table, col);
        int i = 0;
        for (int j = shlist.size(); i < j; i++) {
            SharedFile shfile = shlist.get(i);
          //  File file = new File(shfile.getPath());
            modelo.addRow(new Object[]{shfile.getFilename(),shfile.getOwner(), new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").
                    format(new Date()).toUpperCase()});

        }
        tableShared.setModel(modelo);
        
    }
    
    /**
     * 
     * @param msgm 
     */
    
    private void setLog(String msgm){
        
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
        txtAreaLog.append(sdf.format(new Date()).toUpperCase() + " =>" + msgm + "\n\r");
        txtAreaLog.setLineWrap(true);
        
    //    System.out.println(sdf.format(new Date()));
        
    }
    
           
  

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnConnect = new javax.swing.JButton();
        btnDisconect = new javax.swing.JButton();
        btnLogin = new javax.swing.JButton();
        btnLogout = new javax.swing.JButton();
        btnRegister = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableShared = new javax.swing.JTable();
        btnDownload = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableLocal = new javax.swing.JTable();
        btnUpload = new javax.swing.JButton();
        btnShare = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtAreaLog = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Server Options"));

        btnConnect.setText("Connect");
        btnConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConnectActionPerformed(evt);
            }
        });

        btnDisconect.setText("Disconect");
        btnDisconect.setEnabled(false);
        btnDisconect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDisconectActionPerformed(evt);
            }
        });

        btnLogin.setText("Login");
        btnLogin.setEnabled(false);
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        btnLogout.setText("Logout");
        btnLogout.setEnabled(false);
        btnLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLogoutActionPerformed(evt);
            }
        });

        btnRegister.setText("Register");
        btnRegister.setEnabled(false);
        btnRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegisterActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(67, 67, 67)
                .addComponent(btnConnect, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(59, 59, 59)
                .addComponent(btnDisconect, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(68, 68, 68)
                .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(66, 66, 66)
                .addComponent(btnLogout, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(63, 63, 63))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnConnect, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnDisconect, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnLogout, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnRegister, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Shared With You"));

        tableShared.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "File Name", "Owner", "Modefied in"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableShared.setColumnSelectionAllowed(true);
        jScrollPane2.setViewportView(tableShared);
        tableShared.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tableShared.getColumnModel().getColumn(0).setResizable(false);
        tableShared.getColumnModel().getColumn(0).setPreferredWidth(350);
        tableShared.getColumnModel().getColumn(1).setResizable(false);
        tableShared.getColumnModel().getColumn(1).setPreferredWidth(300);
        tableShared.getColumnModel().getColumn(2).setResizable(false);
        tableShared.getColumnModel().getColumn(2).setPreferredWidth(104);

        btnDownload.setText("Download");
        btnDownload.setEnabled(false);
        btnDownload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDownloadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 784, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDownload, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 37, Short.MAX_VALUE)
                        .addComponent(btnDownload, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29))))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Your Box"));

        tableLocal.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "File Name", "Upload Date"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tableLocal);
        tableLocal.getColumnModel().getColumn(0).setResizable(false);
        tableLocal.getColumnModel().getColumn(0).setPreferredWidth(350);
        tableLocal.getColumnModel().getColumn(1).setResizable(false);

        btnUpload.setText("New Upload");
        btnUpload.setEnabled(false);
        btnUpload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUploadActionPerformed(evt);
            }
        });

        btnShare.setText("Share");
        btnShare.setEnabled(false);
        btnShare.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShareActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 784, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnUpload, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnShare, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(btnUpload, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnShare, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Log"));

        txtAreaLog.setColumns(20);
        txtAreaLog.setEditable(false);
        txtAreaLog.setRows(5);
        jScrollPane3.setViewportView(txtAreaLog);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDisconectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDisconectActionPerformed
        client.stop();
        btnConnect.setEnabled(true);
        btnLogin.setEnabled(false);
        btnRegister.setEnabled(false);
        btnUpload.setEnabled(false);
    }//GEN-LAST:event_btnDisconectActionPerformed

    private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectActionPerformed
        client.setHOST("192.168.25.56");        
        client.setPORT(12345);           
        
        try {
            client.start();            
            btnLogin.setEnabled(true);
            btnRegister.setEnabled(true);            
            btnConnect.setEnabled(false);
            btnDisconect.setEnabled(true);
            setLog(" Connected to the FTP Server " + client.getHOST());          
            
         
        } catch (UnknownHostException ex) {
            System.out.println("falhou");
            
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            
        } catch (IOException ex) {
            System.out.println("falhou");
            JOptionPane.showMessageDialog(null, "Server OFFLINE ", "Connect To Server", 1);
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_btnConnectActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        loginDialog.addListenerLogin(new ListenerLogin());    
        loginDialog.setVisible(true);
    }//GEN-LAST:event_btnLoginActionPerformed

    private void btnLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLogoutActionPerformed
        btnLogin.setEnabled(true);
        client.stop();
            setLog(" Logout " + client.getHOST());
            try {
            client.start();
            btnUpload.setEnabled(false);
            btnDownload.setEnabled(false);
          
            btnLogout.setEnabled(false);
            } catch (UnknownHostException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
    }//GEN-LAST:event_btnLogoutActionPerformed

    private void btnRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegisterActionPerformed
        RegisterFrame rFrame = new RegisterFrame(client);
        rFrame.setVisible(true);        
    }//GEN-LAST:event_btnRegisterActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        
        System.out.println("Fechou");       
        this.dispose();        
        client.stop();
        
    }//GEN-LAST:event_formWindowClosing

    private void btnUploadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUploadActionPerformed
        
       JFileChooser chooser = new JFileChooser();
        String path;
        int retorno = chooser.showOpenDialog(null);
        if(retorno == JFileChooser.APPROVE_OPTION){
            path = chooser.getSelectedFile().getAbsolutePath();
            client.sendFile(path);
            client.running();
            showServerFiles();
        }else{
                //não abriu
        }
    }//GEN-LAST:event_btnUploadActionPerformed

    private void btnDownloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDownloadActionPerformed
        
        int i = tableShared.getSelectedRow();
        String file = (tableShared.getValueAt(i, 0).toString());
        String owner = (tableShared.getValueAt(i, 1).toString());
        System.out.println("Vou baixar isso " + file);
        if (tableShared.getValueAt(i, 0) == -1) {
            JOptionPane.showMessageDialog(null, "Selecione um Item na Lista ", "Download File", 1);
        } else {          
            client.download(file, owner);
          
            //showFiles();
            //     JOptionPane.showMessageDialog(null, file, "Client Runing", 1);

        }
    }//GEN-LAST:event_btnDownloadActionPerformed

    private void btnShareActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShareActionPerformed
        int i = tableLocal.getSelectedRow();
        String file = (tableLocal.getValueAt(i, 0).toString());
        System.out.println("Vou baixar isso " + file);
        if (tableLocal.getValueAt(i, 0) == -1) {
            JOptionPane.showMessageDialog(null, "Selecione um Item na Lista ", "Sharing File", 1);
        } else {            
            new SharingFrame(client, file).setVisible(true);
              client.running();
                showSharedFiles();

        }
        
    }//GEN-LAST:event_btnShareActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConnect;
    private javax.swing.JButton btnDisconect;
    private javax.swing.JButton btnDownload;
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnLogout;
    private javax.swing.JButton btnRegister;
    private javax.swing.JButton btnShare;
    private javax.swing.JButton btnUpload;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tableLocal;
    private javax.swing.JTable tableShared;
    private javax.swing.JTextArea txtAreaLog;
    // End of variables declaration//GEN-END:variables
}
